﻿define(["common"], function (common) {

    common.factory("authenticationService", ["$q", "$http", "$rootScope", function ($q, $http, $rootScope) {
        var signOut = function () {
            var deferred = $q.defer();
            $http.get("/Dashboard/Services/Account/SignOut").
                success(function (data, status, headers, config) {
                    deferred.resolve();
                }).
                error(function (data, status, headers, config) {
                    deferred.reject();
                });

            return deferred.promise;
        };

        var defers = {};
        var $this = {
            signIn: function (username, password, rememberMe) {
                var deferred = $q.defer();

                $http.post("/Dashboard/Services/Account/SignIn", {
                    username: username,
                    password: password,
                    rememberMe: rememberMe
                }).
                    success(function (data, status, headers, config) {
                        deferred.resolve({
                            data: data,
                            status: status,
                            header: header
                        });
                    }).
                    error(function (data, status, headers, config) {
                        deferred.reject({
                            data: data,
                            status: status,
                            header: header
                        });
                    });

                return deferred.promise;
            },

            signOut: signOut,

            register: function () { },

            changePassword: function () { },

            // gets the list of deferred objects by a key
            getDefers: function (key) {
                if (!defers[key]) {
                    defers[key] = [];
                }

                return defers[key];
            },

            // resolve all deferred objects of a key
            resolve: function (key, result) {
                var defers = $this.getDefers(key);
                if (!defers) return;

                var deferred = defers.shift();
                while (deferred) {
                    deferred.resolve(result);
                    deferred = defers.shift();
                }
            },

            // reject all deferred objects of a key
            reject: function (key, result) {
                var defers = $this.getDefers(key);
                if (!defers) return;

                var deferred = defers.shift();
                while (deferred) {
                    deferred.reject(result);
                    deferred = defers.shift();
                }
            }
        };

        return $this;
    }]);

    common.factory("signInService", ["$q", "$dialog", "authenticationService", function ($q, $dialog, authenticationService) {
        var signIn = {
            dialog: null,
            defers: [],
            ensure: function () {
                if (signIn.dialog) return;

                signIn.dialog = $dialog.dialog({
                    backdrop: true,
                    keyboard: false,
                    backdropClick: false,
                    dialogClass: 'sign-in',
                    resolve: {
                    },
                    templateUrl: '/Modules/NineSeas.Dashboard/Views/Template/SignIn.html',
                    controller: 'signInController'
                });
            },

            showDialog: function () {
                signIn.ensure();

                if (!signIn.dialog.isOpen()) {
                    signIn.dialog.open();
                }
            },

            closeDialog: function () {
                if (signIn.dialog.isOpen()) {
                    signIn.dialog.close();
                }
            }
        };
        return signIn;
    }]);

    common.controller("signInController", [
        "$scope",
        "$window",
        "$document",
        "authenticationService",
        "signInService",
        "notifyService",
        function ($scope, $window, $document, authenticationService, signInService, notifyService) {
            var mWindow = angular.element($window);

            // calculate the position of the dialog
            var calculatePosition = function () {
                var windowHeight = mWindow.height();
                var top = (windowHeight - 360) / 2;
                if (top < 10) top = 10;
                $scope.top = top;
            };
            calculatePosition();
            mWindow.resize(calculatePosition);

            $scope.submit = function () {
                authenticationService.signIn($scope.username, $scope.password, $scope.rememberMe)
                    .then(
                        function (result) {
                            authenticationService.resolve("signIn", result);
                            signInService.closeDialog();

                            notifyService.message("Signed in!");
                        },
                        function (result) {
                            authenticationService.reject("signIn", result);
                        }
                    );
            };
        }
    ]);

});